package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.PriceData;
import com.zdd.ehe.entity.Produce;
import com.zdd.ehe.entity.ProvinceProduceData;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IProvinceProduceDataService;
import com.zdd.ehe.service.ProduceService;
import com.zdd.ehe.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-24
 */
@Api(tags="各省份作物产量数据管理模块")
@RestController
@RequestMapping("/provinceProduceData")
public class ProvinceProduceDataController {
    @Resource
    private IProvinceProduceDataService provinceProduceDataService;
    @Resource
    private ProduceService produceService;

    @ApiOperation("分页获取各省份作物产量数据接口")
    @PostMapping("/getallprovinceproducebypage")
    public Result getAllProvinceProduceByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page<ProvinceProduceData> pagex = new Page<>(page,pageSize);
        Page<ProvinceProduceData> pageResult = provinceProduceDataService.getAllProvinceProduceByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }



    @ApiOperation("各省份某种作物产量数据接口")
    @PostMapping("/getProvinceProduceByType")
    public Result getProvinceProduceByType(@RequestParam("type") String type){
        List<ProvinceProduceData> provinceProduceDataList = provinceProduceDataService.getProvinceProduceByType(type);
        if(provinceProduceDataList.size() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",provinceProduceDataList);
    }


    @ApiOperation("通过产品类型获取产量与销量数据")
    @PostMapping("/queryProduceAndSaleByType")
    public Result queryProduceAndSaleByType(@RequestParam("type") String type){

       List<Produce> produceList = produceService.queryProduceAndSaleByType(type);
        if(produceList.size() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",produceList);
    }


    @ApiOperation("根据作物品种查询接口")
    @GetMapping("/getprovinceproducedatabytype")
    public Result getProvinceProduceDataByType(@RequestParam("type") String type){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("type",type);
        List<ProvinceProduceData> provinceProduceDataList = provinceProduceDataService.list(queryWrapper);
        if(provinceProduceDataList.size() != 0){
            Map<String,Object> data = new HashMap<>();
            data.put("provinceProduceDataList",provinceProduceDataList);
            data.put("total",provinceProduceDataList.size());
            data.put("size",10);
            data.put("current",1);
            data.put("pages",1);
            return Result.success("查询成功!",data);
        }else{
            return Result.error(4001,"未查询到该角色");
        }
    }


    @ApiOperation("添加作物产量")
    @PostMapping("/addprovinceproducedata")
    public Result addProvinceProduceData(@RequestBody ProvinceProduceData provinceProduceData){
        int r = provinceProduceDataService.addProvinceProduceData(provinceProduceData);
        if(r == 1){
            return Result.success("添加成功");
        }else {
            return Result.error(4002,"添加失败");
        }
    }



    @ApiOperation("删除接口")
    @PostMapping("/deleteprovinceproducedatabyid")
    public Result deleteProvinceProduceDataById(@RequestParam("id") int id){
        ProvinceProduceData provinceProduceData = provinceProduceDataService.getById(id);
        if(provinceProduceData == null){
            return Result.error(4001,"编号错误");
        }
        provinceProduceData.setStatus("0");
        int result = provinceProduceDataService.updateProvinceProduceDataById(provinceProduceData);
        if(result == -1){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");
    }


    @ApiOperation("重启接口")
    @PostMapping("/restartprovinceproducedatabyid")
    public Result restartProvinceProduceDataById(@RequestParam int id){
        ProvinceProduceData provinceProduceData = provinceProduceDataService.getById(id);
        if(provinceProduceData == null){
            return Result.error(4001,"编号错误");
        }
        provinceProduceData.setStatus("1");
        int result = provinceProduceDataService.updateProvinceProduceDataById(provinceProduceData);
        if(result == -1){
            return Result.error(4002,"重启失败");
        }
        return Result.success("重启成功");
    }


    @ApiOperation("修改作物产量接口")
    @PostMapping("/updateprovinceproducedata")
    public Result updateProvinceProduceData(@RequestBody ProvinceProduceData provinceProduceData){
        int result = provinceProduceDataService.updateProvinceProduceDataById(provinceProduceData);
        if(result == -1){
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

}
